#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 1e4 + 5;
const int M = 5e4 + 5;
const int Q = 3e4 + 5;

int n, m;
int a[N];
vector<pii> ch[N];
struct Edge {
  int x, y, z;
  bool operator<(Edge o) const { return z > o.z; }
} e[M];
struct Query {
  int x, y;
} q[Q];
int f[N];
void init() { rep(i, 1, n) f[i] = i; }
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
bool merge(int x, int y) {
  int fx = find(x), fy = find(y);
  if (fx == fy) return false;
  f[fy] = fx;
  return true;
}
void kruskal() {
  init();
  sort(e + 1, e + m + 1);
  rep(i, 1, m) {
    if (merge(e[i].x, e[i].y)) {
      ch[e[i].x].push_back(pii(e[i].y, e[i].z));
      ch[e[i].y].push_back(pii(e[i].x, e[i].z));
    }
  }
  rep(i, 1, n) {
    if (f[i] == i) {
      ch[0].push_back(pii(i, -1));
      ch[i].push_back(pii(0, -1));
    }
  }
}
int st[N][14], mn[N][14], dep[N];
void dfs(int u, int fu, int w) {
  st[u][0] = fu, mn[u][0] = w, dep[u] = dep[fu] + 1;
  for (pii p : ch[u]) {
    int v = p.first;
    if (v == fu) continue;
    dfs(v, u, p.second);
  }
}
void init_st() {
  rep(j, 1, 13) {
    rep(i, 1, n) {
      st[i][j] = st[st[i][j - 1]][j - 1];
      mn[i][j] = min(mn[i][j - 1], mn[st[i][j - 1]][j - 1]);
    }
  }
}
int lca(int u, int v) {
  int res = 1e9;
  if (dep[u] < dep[v]) swap(u, v);
  int d = dep[u] - dep[v];
  per(j, 13, 0) {
    if (d & (1 << j)) {
      res = min(res, mn[u][j]);
      u = st[u][j];
    }
  }
  if (u == v) return res;
  per(j, 13, 0) {
    if (st[u][j] != st[v][j]) {
      res = min({res, mn[u][j], mn[v][j]});
      u = st[u][j];
      v = st[v][j];
    }
  }
  res = min({res, mn[u][0], mn[v][0]});
  return res;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  rep(i, 1, m) cin >> e[i].x >> e[i].y >> e[i].z;
  kruskal();
  dfs(0, 0, 0);
  init_st();
  int t;
  cin >> t;
  while (t--) {
    int u, v;
    cin >> u >> v;
    cout << lca(u, v) << endl;
  }
  return 0;
}